#include"..\..\..\include\hge.h"

HGE* hge = NULL;


hgeQuad quad;
HEFFECT snd;

float x = 100.0f, y = 100.0f;
float dx = 0.0f, dy = 0.0f;

const float speed = 90;
const float friction = 0.98f;

void boom()
{
	int pan = int((x - 400)/4);
	float pitch = (dx*dx+dy*dy)*0.0005f+0.2f;
}

bool FrameFunc()
{
	float dt = hge->Timer_GetDelta();
	
	if(hge->Input_GetKeyState(HGEK_ESCAPE))
		return true;

	if(hge->Input_GetKeyState(HGEK_LEFT))
		dx -= speed*dt;

	if(hge->Input_GetKeyState(HGEK_RIGHT))
		dx += speed*dt;

	if(hge->Input_GetKeyState(HGEK_UP))
		dy -= speed*dt;

	if(hge->Input_GetKeyState(HGEK_DOWN))
		dy += speed*dt;

	dx *= friction;
	dy *= friction;
	x += dx;
	y += dy;

	if(x>784)
	{
		x = 784 - (x - 784);	dx = -dx;	boom();
	}

	if(x < 16)
	{
		x = 16 + 16 - x;	dx = -dx;	boom();
	}

	if( y > 584)
	{
		y = 584 - (y-584);	dy = -dy;	boom();
	}

	if( y < 16)
	{
		y = 16 + 16 - y;	dy = -dy;	boom();
	}

	
	quad.v[0].x = x - 16;		quad.v[0].y = y - 16;
	quad.v[1].x = x + 16;		quad.v[1].y = y - 16;
	quad.v[2].x = x + 16;		quad.v[2].y = y + 16;
	quad.v[3].x = x - 16;		quad.v[3].y = y + 16;


	return false;
}

bool RenderFunc()
{
	hge->Gfx_BeginScene();
	hge->Gfx_Clear(0);
	hge->Gfx_RenderQuad(&quad);
	hge->Gfx_EndScene();

	return false;
}

int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
	hge = hgeCreate(HGE_VERSION);

	hge->System_SetState(HGE_LOGFILE,"test.log");
	hge->System_SetState(HGE_FRAMEFUNC,FrameFunc);
	hge->System_SetState(HGE_RENDERFUNC,RenderFunc);
	hge->System_SetState(HGE_TITLE,"hello world");
	
	hge->System_SetState(HGE_WINDOWED,true);
	hge->System_SetState(HGE_SCREENWIDTH,800);
	hge->System_SetState(HGE_SCREENHEIGHT,600);
	hge->System_SetState(HGE_SCREENBPP,32);

	
	hge->System_SetState(HGE_USESOUND,false);
	

	




	if(hge->System_Initiate())
	{
		quad.tex = hge->Texture_Load("particles.png");
		quad.blend = BLEND_ALPHAADD |BLEND_COLORMUL|BLEND_ZWRITE;
		
	

		for( int i = 0;i<4;i++)
		{
			quad.v[i].z = 0.5f;
			quad.v[i].col = 0XFFFFA000;
		}

		quad.v[0].tx = 96.0/128.0;	quad.v[0].ty = 64.0/128.0;
		quad.v[1].tx = 128.0/128.0;	quad.v[1].ty = 64.0/128.0;
		quad.v[2].tx = 128.0/128.0;	quad.v[2].ty = 96.0/128.0;
		quad.v[3].tx = 96.0/128.0;	quad.v[3].ty = 96.0/128.0;


		hge->System_Start();			//	main loop

		hge->Texture_Free(quad.tex);
	}
	else
	{
		MessageBox(NULL,TEXT("hge initiate() error"),TEXT("error"),MB_OK|MB_ICONERROR|MB_APPLMODAL);
	}

	

	hge->System_Shutdown();
	hge->Release();

	return 0;
}